package br.ucb.gafs.jimagefun.facedetector;

import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.util.List;

import jjil.algorithm.RgbAvgGray;
import jjil.core.RgbImage;
import jjil.j2se.RgbImageJ2se;

public class FaceDetector {

	/**
	 * Retorna uma lista de retangulos com as faces encontradas na imagem passada como parâmetro
	 * @param bi BufferedImage onde as faces serão procuradas
	 * @param minScale Minimum (finest) scale at which features will be detected.
	 * @param maxScale Maximum (coarsest) scale at which features will be detected.
	 * @return
	 */
    public static List<Rectangle> findFaces(BufferedImage bi, int minScale, int maxScale) {
        try {
            InputStream is  = FaceDetector.class.getResourceAsStream("HCSB.txt");
            Gray8DetectHaarMultiScale detectHaar = new Gray8DetectHaarMultiScale(is, minScale, maxScale);
            RgbImage im = RgbImageJ2se.toRgbImage(bi);
            RgbAvgGray toGray = new RgbAvgGray();
            toGray.push(im);
            
            return detectHaar.pushAndReturn(toGray.getFront());
        } catch (Throwable e) {
            throw new IllegalStateException(e);
        }
    }
}
